package com.zmm.study.order.listener;

import com.rabbitmq.client.Channel;
import com.zmm.study.order.model.MoNiOrder;
import com.zmm.study.order.service.OrderService;
import com.zmm.util.time.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 超过延时队列的时间 进入关闭订单队列
 * @author 900045
 * @description:
 * @name OrderCloseListener
 * @date By 2021-10-19 20:03:16
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
@Slf4j
public class OrderCloseListener {

	@Resource
	private OrderService orderService;

	/**
	 * 接受延时队列 【CREATE_ORDER】
	 * 监听队列 		order.delay.queue		/ order.release.order.queue		/ stock.release.stock.queue
	 * Binding 
	 * routingKey	order.create.order		/ order.release.order			/ order.release.other.#
	 * exchange		order-event-exchange	/ order-event-exchange			/ order-event-exchange
	 * @author: 900045
	 * @date: 2021-10-21 10:51:47
	 * @throws 
	 * @param moNiOrder: 
	 * @param channel: 
	 * @param message: 
	 * @return: void
	 **/
	@RabbitHandler
	public void listenerOrder(MoNiOrder moNiOrder, Channel channel, Message message) throws IOException {
		try {
			log.info("{} 收到的超时订单号:{}", DateUtil.getNow(),moNiOrder.getOrderNo());
			orderService.closeOrder(moNiOrder);
			// 1.释放订单服务 2.优惠券返回服务 3.解锁库存服务
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (Exception e){
			channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
		}

	}
}
